package com.iyao_app.im.adapter;

import android.app.Activity;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.MyInterestBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xue on 2017/11/25.
 */

public class AttentionFriendAdapter extends RecyclerView.Adapter<AttentionFriendAdapter.Friend> {

    private Activity context;
    private RecyclerView recyclerView;
    private ONCLICK onclick;
    public List<MyInterestBean> list = new ArrayList<>();
    private LayoutInflater inflater;

    public AttentionFriendAdapter(Activity context, RecyclerView recyclerView, List<MyInterestBean> list, ONCLICK onclick) {
        this.context = context;
        this.recyclerView = recyclerView;
        this.onclick = onclick;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    public interface ONCLICK {
        void onclick(int position);
    }

    @Override
    public Friend onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_friend, null);
        return new Friend(view);
    }

    @Override
    public void onBindViewHolder(Friend holder,final int position) {
        holder.friend_name.setText(list.get(position).getNickName());
//        Glide.with(context).load(list.get(position).getFavicon()).centerCrop().into(holder.head_portrait);
        try {
            Uri uri = Uri.parse(list.get(position).getFavicon());
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(100, 100))
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                    .setOldController(holder.head_portrait.getController())
                    .setImageRequest(request)
                    .build();
            holder.head_portrait.setController(controller);
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.friend_message.setText(String.valueOf(list.get(position).getFriendId()));
        int sex = list.get(position).getSex();
        if (sex != 0){
            if (sex == 1){
                Glide.with(context).load(R.mipmap.ic_boy).centerCrop().into(holder.friend_sex);
            }else if (sex == 2){
                Glide.with(context).load(R.mipmap.ic_girl).centerCrop().into(holder.friend_sex);
            }
        }else {

        }
        holder.chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (onClickListener != null) {
                    onClickListener.chat(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    public void setOnChatListener(OnChatListener listener) {
        onClickListener = listener;
    }

    private OnChatListener onClickListener;

    public interface OnChatListener {
        void chat(int position);
    }

    class Friend extends RecyclerView.ViewHolder {


        SimpleDraweeView head_portrait;
        TextView friend_name;
        TextView friend_message;
        ImageView friend_sex;
        Button chat;

        public Friend(View itemView) {
            super(itemView);
            head_portrait = (SimpleDraweeView)itemView.findViewById(R.id.head_portrait);
            friend_name = (TextView) itemView.findViewById(R.id.friend_name);
            friend_message = (TextView) itemView.findViewById(R.id.friend_message);
            friend_sex = (ImageView) itemView.findViewById(R.id.friend_sex);
            chat = (Button) itemView.findViewById(R.id.chat);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int i = recyclerView.getChildPosition(view);
                    onclick.onclick(i);
                }
            });
        }
    }
}
